package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class test {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 设置发送到交换机的回调函数(无论信息收到还是收不到都可以执行):
     * 1. 配置文件连接工厂设置publisher-confirms="true"
     * 2. 设置回调函数
     * 3. 发送消息
     */
    @Test
    public void test1(){
        /**
         * 设置发送到交换机的回调函数
         * ask 是否成功发送到交换机
         * cause 失败的原因
         */
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if(ack){
                    System.out.println("交换机收到信息"+cause);
                }else {
                    System.out.println("交换机未收到信息"+cause);
                }
            }
        });

        rabbitTemplate.convertAndSend("test_exchange_confirm","confirm","测试交换机回调方法执行情况1");
    }

    /**
     * 设置从交换机发送到队列的回调函数(当信息收不到的时候才可能执行):
     * 1. 配置文件连接工厂设置publisher-returns="true"
     * 2. 回调函数默认是自动丢弃消息不执行回调方法,设置 rabbitTemplate.setMandatory(true)执行回调方法
     * 3. 设置回调函数
     * 4. 发送消息
     */
    @Test
    public void test2(){
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//                System.out.println(message);
//                System.out.println(replyCode);
//                System.out.println(replyText);
//                System.out.println(exchange);
//                System.out.println(routingKey);
                System.out.println("回调执行了");
            }
        });


        rabbitTemplate.convertAndSend("test_exchange_confirm","1","sss");
    }

    @Test
    public void send1(){
        for (int i = 0; i <=10 ; i++) {
            rabbitTemplate.convertAndSend("test_exchange_confirm","confirm",""+i);
        }
    }

    /**
     * 设置队列里的消息的过期时间有两种方式:队列整体设置和消息单独设置
     * 消息单独设置过期时间时,消息必须要在队列的顶端,即消费者最先收到的信息,才可以生效
     * 一般来说都是统一设置队列的过期时间
     */
    @Test
    public void send2(){
//        for (int i = 0; i <=10 ; i++) {
//            rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe",""+i);
//        }
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息的过期时间
                message.getMessageProperties().setExpiration("5000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","测试过期",messagePostProcessor);
    }

    @Test
    public void testDlx(){
        //测试过期时间产生的死信消息
//        rabbitTemplate.convertAndSend("exchange_dlx","dlx","死信测试");

        for (int i = 0; i < 21; i++) {
            rabbitTemplate.convertAndSend("exchange_dlx","dlx","死信测试");
        }
    }

    @Test
    public void testOrder() throws InterruptedException {
        rabbitTemplate.convertAndSend("exchange_order","order","延时测试");

        for (int i = 1; i <= 10; i++) {
            Thread.sleep(1000);
            System.out.println(i);
        }
    }
}
